//
//  ZBWAlbumModel.m
//  ZBWAlbum
//
//  Created by 张博文 on 2020/3/19.
//  Copyright © 2020 com.bw. All rights reserved.
//

#import "ZBWAlbumModel.h"

@implementation ZBWAlbumModel
-(void)setCollection:(PHAssetCollection *)collection {
    _collection = collection;
    
    if ([collection.localizedTitle isEqualToString:@"All Photos"]) {
        self.collectionTitle = @"全部相册";
    } else {
        self.collectionTitle = collection.localizedTitle;
    }
    
    PHFetchOptions *option = [[PHFetchOptions alloc] init];
    if (!self.sortAscendingByModificationDate) {
        option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:self.sortAscendingByModificationDate]];
    }
    
    self.collectionTitle = collection.localizedTitle;
    
    // 获得某个相簿中的所有PHAsset对象
    //self.assets = [PHAsset fetchAssetsInAssetCollection:collection options:option];

    if ([collection isKindOfClass:[PHAssetCollection class]]) {
        self.assets = [PHAsset fetchAssetsInAssetCollection:(PHAssetCollection *)collection options:option];
    } else if ([collection isKindOfClass:[PHCollectionList class]]) {
        // 处理 PHCollectionList 的情况
        NSLog(@"警告：传入了 PHCollectionList 而不是 PHAssetCollection");
        if ([collection isKindOfClass:[PHCollectionList class]]) {
            PHCollectionList *collectionList = (PHCollectionList *)collection;
            PHFetchResult *collections = [PHCollection fetchCollectionsInCollectionList:collectionList options:nil];
            for (NSUInteger i = 0; i < collections.count; i++) {
                PHCollection *subCollection = [collections objectAtIndex:i];
                if ([subCollection isKindOfClass:[PHAssetCollection class]]) {
                    PHAssetCollection *assetCollection = (PHAssetCollection *)subCollection;
                    self.assets = [PHAsset fetchAssetsInAssetCollection:assetCollection options:option];
                    break; // 相当于在 block 中设置 *stop = YES;
                }
            }
        }
    } else {
        NSLog(@"错误：未知的集合类型");
    }

    if (self.assets.count > 0) {
        self.firstAsset = self.assets[0];
    }
    self.collectionNumber = [NSString stringWithFormat:@"%ld", self.assets.count];
}


#pragma mark - Get方法
-(NSMutableArray<NSNumber *> *)selectRows {
    if (!_selectRows) {
        _selectRows = [NSMutableArray array];
    }
    
    return _selectRows;
}

@end
